<script>
class Dog {
  breed = null;
  size = null;
  color = '';
  age = 0;
  constructor(breed, size, color, age) {
    this.breed = breed
    this.size = size
    this.color = color
    this.age = age
  }
  run() {
    console.log(this.getName() + ' 在奔跑。')
  }
  eat() {
    console.log(this.getName() + ' 在吃东西。')
  }
  sleep() {
    console.log(this.getName() + ' 在睡觉。')
  }
  play() {
    console.log(this.getName() + ' 在玩。')
  }

  getName() {
    return this.color + '的' + this.breed
  }
  toString() {
    return this.getName() + `今年${this.age}岁`
  }
}
let dog1 = new Dog('斗牛犬', '中等', '黑色', 3)
// dog1 instanceof Dog 如果返回true，dog1是Dog的实例，Dog是dog1的类

console.log(dog1 instanceof Dog)
dog1.run()
dog1.eat()
dog1.sleep()

// ----

let dog2 = new Dog()
// dog2 instanceof Dog 如果返回true，dog2是Dog的实例，Dog是dog2的类
dog2.breed = '柯基'
dog2.size = '小型'
dog2.color = '黄色'
dog2.age = 10

console.log(dog2 instanceof Dog)
dog2.run()
dog2.eat()
dog2.sleep()


// function Point(x, y) {
//   this.x = x;
//   this.y = y;
// }
// Point.prototype.moveBy = function (vx, vy) {
//   this.x += vx
//   this.y += vy
// };
// cooker
// player

const obj = {}
obj.x = 1

class Point {
  x = 0
  y = 0
  constructor(x, y) {
    this.x = x; // 成员属性，实例属性
    this.y = y;
  }
  moveBy(vx, vy) { // 成员方法
    this.x += vx
    this.y += vy
  }
}
const p = new Point(1,2)

class A {
  // constructor() {}
  x() {
    // "use strict"

  }
}

function abc() {
  "use strict"
}


class Rectangle {
  #height = 0;
  #width;
  constructor(height, width) {
    this.#height = height;
    this.#width = width;
  }
  setWidth(v) {
    if (!Number.isInteger(v) || v <= 0 || v>1000) {
      // return
      throw new Error('width必须在0-1000之间的整数')
    }
    this.#width = v
  }
  setHeight(v) {
    if (!Number.isInteger(v) || v <= 0 || v>800) {
      // return
      throw new Error('height必须在0-800之间的整数')
    }
    this.#height = v
  }
  getArea() { return this.#width * this.#height }
}
const r = new Rectangle(100, 100)
// console.log(r.#width )
console.log(r.getArea())
r.setWidth(700)
r.setHeight(100)
console.log(r.getArea())

// function MyClass() {
//   this.type = 'xyz'
// }
// MyClass.type = 'abc'
// const o = new MyClass()
// console.log(o.type, MyClass.type)
// o.__proto__ === MyClass.prototype

class MyClass {
  static type = 'abc'
  constructor() {
    this.type ='xyz'
  }
}

class User {
  static #MAX_INSTANCES = 2;
  static #instances = 0;

  name;
  constructor(name) {
    User.#instances++;
    if (User.#instances > User.#MAX_INSTANCES) {
      throw new Error('不能创建用户');
    }
    this.name = name;
  }

  static log() {
    console.log(this.#instances)
  }
}

User.log()

</script>